import {
    isTrue,
    isPrimitive,
    isDef,
    resolveAsset
} from '../util/index.js'
import { createComponent } from './create-component'
import { normalizeChildren } from './helpers/normalize-children'
import config from '../config'
import VNode from './vnode'
const SIMPLE_NORMALIZE = 1;
const ALWAYS_NORMALIZE = 2;
export function createElement(
    context,
    tag,
    data,
    children,
    normalizationType,
    alwaysNormalize
) {
    console.log('createElement方法',tag,data,children)
    //满足一下条件变量前移
    if (Array.isArray(data) || isPrimitive(data)) {
        normalizationType = children
        children = data
        data = undefined
    }
    if (isTrue(alwaysNormalize)) {
        normalizationType = ALWAYS_NORMALIZE
    }
    return _createElement(context, tag, data, children, normalizationType)
}
export function _createElement (
    context,
    tag,
    data,
    children,
    normalizationType
) {
    if (normalizationType === ALWAYS_NORMALIZE) {
        //children转换成数组TextVNode
        console.log("children转换成数组TextVNode")
        children = normalizeChildren(children);
    } else if (normalizationType === SIMPLE_NORMALIZE) {

    }
    let vnode
    if (typeof tag === 'string') {
        let Ctor
        if (config.isReservedTag(tag)) {
            vnode = new VNode(
                config.parsePlatformTagName(tag), data, children,
                undefined, undefined, context
            )
        } else if (!data && isDef(Ctor = resolveAsset(context.$options, 'components', tag))) {
            //初始化组件vnode
            vnode = createComponent(Ctor, data, context, children, tag);
        } else {
            //生成vnode
            vnode = new VNode(
                tag, data, children,
                undefined, undefined, context
            )
            console.log("生成vnode",vnode)
        }
    }
    return vnode
}